<?php
/*
 * 广告
 */
class AdMode extends SiteMode {
    /** @return AdMode */
    public static function init() {
        return parent::init();
    }
    function getList($rows = 30,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql = "SELECT SQL_CALC_FOUND_ROWS A.*,P.`url` as `picture_url` FROM `ad` A left join `picture` P on A.`picture_id`=P.`id` 
        where site_id=? order by A.`create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql,$this->siteId);
    }
    function getDetail($id){
        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `ad` WHERE `id`=$id";
        return $this->db->getOne($sql);
    }
    function add($picture_id,$url,$start_time = null,$end_time = null){
        $data['picture_id']=$picture_id;
        $data['url']=$url;
        $data['start_time']=$start_time;
        $data['end_time']=$end_time;
        $data['site_id']=$this->siteId;
        return $this->db->insert('ad',$data) == 1 ? (int)$this->db->insertId():-1;
    }
    function modify($id,$picture_id,$url,$start_time = null,$end_time = null){
        $id=(int)$id;
        $data['picture_id']=$picture_id;
        $data['url']=$url;
        $data['start_time']=$start_time;
        $data['end_time']=$end_time;
        return $this->db->update('ad',$id,$data) == 1 ;
    }
    function delete($id){
        return $this->db->delete('ad',$id) == 1;
    }
}